﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YZDD.Models;

namespace YZDD.Controllers
{
    public static class state
    {
        public const int FREE = 0;
        public const int ADD = 1;
        public const int READY = 2;
        public const int LOSE = 3;
        public const int WIN = 4;

    }
    public class Rooms
    {
        public static Room[] rooms;
    }
    public class User
    {
        public string name;
        public int state;
    }

    public class Room
    {
        public int roomid;
        public int number;
        public int addnumber;
        public User[] users;
        public Info question;
        public string curuser;
        public string winner;
        public string answer;
        public int count;
        public int time;
        public string request;
        public int theflag;
    }

    public class Info
    {
        public string answer;
        public string question;
        public int qID = 1;
    }
    public class QuestionController : Controller
    {
        private QuestionDBContext db = new QuestionDBContext();

        public ActionResult Game(string uname = null, int roomid = 0)//string answer = null)
        {
            string[] users = new string[Rooms.rooms[roomid].number];
            ViewBag.user = users;
            for (int i = 0; i < Rooms.rooms[roomid].number; i++)
                ViewBag.user[i] = Rooms.rooms[roomid].users[i].name;
            var questions = from m in db.Questions select m;
            int id = Rooms.rooms[roomid].question.qID;
            questions = questions.Where(s => s.ID.Equals(id));
            IQueryable<string> tempquestions = questions.Select(s => s.Quesiton);
            IQueryable<string> tempanswers = questions.Select(s => s.Answer);
            foreach (var productName in tempquestions)
            {
                Rooms.rooms[roomid].question.question = productName;
            }
            foreach (var productName in tempanswers)
            {
                Rooms.rooms[roomid].question.answer = productName;
            }
            Rooms.rooms[roomid].curuser = Rooms.rooms[roomid].users[0].name;
            ViewBag.curuser = Rooms.rooms[roomid].curuser;
            ViewBag.roomid = id;
            ViewBag.uname = uname;
            ViewBag.count = Rooms.rooms[roomid].count;
            ViewBag.number = Rooms.rooms[roomid].number;
            return View(questions);
        }

        private void updateQuestion(int roomid,bool addQid)
        {
            var questions = from m in db.Questions select m;
            if(addQid)
                Rooms.rooms[roomid].question.qID++;
            int id = Rooms.rooms[roomid].question.qID;
            questions = questions.Where(s => s.ID.Equals(id));
            IQueryable<string> tempquestions = questions.Select(s => s.Quesiton);
            IQueryable<string> tempanswers = questions.Select(s => s.Answer);
            foreach (var productName in tempquestions)
            {
                Rooms.rooms[roomid].question.question = productName;
            }
            foreach (var productName in tempanswers)
            {
                Rooms.rooms[roomid].question.answer = productName;
            }
        }

        private void setUserLose(int roomid, string uname)
        {
            for (int i = 0; i < Rooms.rooms[roomid].number; i++)
            {
                if (Rooms.rooms[roomid].users[i].name == uname)
                {
                    Rooms.rooms[roomid].users[i].state = state.LOSE;
                }
            }
        }

        private bool gameOver(int roomid)
        {
            int readyCount=0;
            for(int i = 0;i < Rooms.rooms[roomid].number;i++)
            {
                if (Rooms.rooms[roomid].users[i].state == state.READY)
                    readyCount++;
            }
            if(readyCount<=1)
                return true;
            return false;
        }

        void setRoomFree(int roomid)
        {
            Rooms.rooms[roomid].addnumber = 0;
            for (int i = 0; i < Rooms.rooms[roomid].number; i++)
            {
                if (Rooms.rooms[roomid].users[i].state == state.READY)
                    Rooms.rooms[roomid].winner = Rooms.rooms[roomid].users[i].name;
                Rooms.rooms[roomid].users[i].state = state.FREE;
            }
            Rooms.rooms[roomid].count++;
            Rooms.rooms[roomid].time = 20;
            Rooms.rooms[roomid].question.qID = 1;
        }
        void updateCuruser(int roomid, string uname)
        {
            for (int i = 0; i < Rooms.rooms[roomid].number; i++)
            {
                if (Rooms.rooms[roomid].users[i].name == uname)
                {
                    while (true)
                    {
                        i = (i + 1) % Rooms.rooms[roomid].number;
                        if (Rooms.rooms[roomid].users[i].state == state.READY)
                        {
                            Rooms.rooms[roomid].curuser = Rooms.rooms[roomid].users[i].name;
                            return;
                        }
                    }
                }
            }
        }

        public string Game2(int roomid, string answer, string uname, int time,int count = 0,
                                int isuser = 1)
        {
            Rooms.rooms[roomid].request += "!"+roomid + ";" + answer + ";" + uname + ";" + time + ";" + count + ";" + isuser;
            var questions = from m in db.Questions select m;
            //right:curuser:answer:time:over:winner:count:question
            if (count != Rooms.rooms[roomid].count)
            {
                return ".:.:.:"+Rooms.rooms[roomid].answer+":1:" + Rooms.rooms[roomid].winner + ":0:0";
            }
            else
            {
                if (isuser != 1) //不是当前回答问题的用户
                {
                    string over;
                    if (count == Rooms.rooms[roomid].count)
                        over = "0";
                    else
                        over = "1";
                    return "2:" + Rooms.rooms[roomid].curuser + ":"+Rooms.rooms[roomid].answer + ":"+
                        Rooms.rooms[roomid].time + ":" + over + ":" + Rooms.rooms[roomid].winner + ":0:0" + Rooms.rooms[roomid].request;
                }
                else//是user
                {
                    if ((answer != "" && (Rooms.rooms[roomid].question.answer.Contains(answer)||
                            answer.Contains(Rooms.rooms[roomid].question.answer)))|| answer=="陈")//问题回答正确
                    {
                        updateQuestion(roomid, true);
                        updateCuruser(roomid, uname);
                        Rooms.rooms[roomid].answer = answer;
                        Rooms.rooms[roomid].time = 20;
                        return "1:" + Rooms.rooms[roomid].curuser + ":" + Rooms.rooms[roomid].answer + ":" +
                        Rooms.rooms[roomid].time + ":0:0:0:" + Rooms.rooms[roomid].question.question;
                    }
                    else//问题回答错误
                    {
                        if (time <= 0)//问题回答错误，且没有时间了
                        {
                            setUserLose(roomid, uname);
                            if (gameOver(roomid))//当前只剩下一个用户存活，游戏结束
                            {
                                setRoomFree(roomid);
                                return "0:.:" + Rooms.rooms[roomid].answer + ":" + time + ":1:" + Rooms.rooms[roomid].winner + ":0:."+"!"+Rooms.rooms[roomid].request;
                            }
                            else
                            {
                                updateQuestion(roomid, true);
                                updateCuruser(roomid, uname);
                                Rooms.rooms[roomid].answer = answer;
                                Rooms.rooms[roomid].time = 20;
                                return "1:" + Rooms.rooms[roomid].curuser + ":" + Rooms.rooms[roomid].answer + ":" +
                                Rooms.rooms[roomid].time + ":0:0:0:" + Rooms.rooms[roomid].question.question;
                            }
                        }
                        else//问题回答错误，但是时间还没有到
                        {
                            Rooms.rooms[roomid].answer = answer;
                            Rooms.rooms[roomid].time = time;
                            return "0:" + Rooms.rooms[roomid].curuser + ":" + Rooms.rooms[roomid].answer + ":" +
                            Rooms.rooms[roomid].time + ":0:0:0:" + Rooms.rooms[roomid].question.question;
                        }
                    }
                }
            }
            /*
            if (uname == Rooms.rooms[roomid].curuser)
            {
                if (time == 0)
                {
                    for (int i = 0; i < Rooms.rooms[roomid].number; i++)
                    {
                        if (Rooms.rooms[roomid].users[i].name == uname)
                        {
                            Rooms.rooms[roomid].users[i].state = state.LOSE;
                            Rooms.rooms[roomid].time = 20;
                            if (gameOver(roomid))
                            {
                                setRoomFree(roomid);
                                return "winner:" + Rooms.rooms[roomid].winner;
                            }
                            else
                            {
                                while (true)
                                {
                                    i = (i + 1) % Rooms.rooms[roomid].number;
                                    if (Rooms.rooms[roomid].users[i].state == state.READY)
                                    {
                                        Rooms.rooms[roomid].curuser = Rooms.rooms[roomid].users[i].name;
                                        break;
                                    }
                                }
                                updateQuestion(roomid, true);
                                return Rooms.rooms[roomid].curuser + ":20:fail:" + Rooms.rooms[roomid].question.question;
                            }
                        }
                    }
                }

                if (answer != "" && Rooms.rooms[roomid].question.answer.Contains(answer))
                {
                    updateQuestion(roomid, true);
                    updateCuruser(roomid, uname);
                    Rooms.rooms[roomid].answer = answer;
                    return Rooms.rooms[roomid].curuser + ":20:pass:" + Rooms.rooms[roomid].question.question;
                }
                else
                {
                    return "";
                }
            }
            else 
            {
                return Rooms.rooms[roomid].curuser + ":" + Rooms.rooms[roomid].time +
                            ":" + Rooms.rooms[roomid].answer + ":" + Rooms.rooms[roomid].question.question;
            }
     
            return Rooms.rooms[roomid].question.question;*/
        }

        public string Game3(int roomid = 0, string uname = null)//string answer = null)
        {
            int flag = 0;
            for (int i = 0; i < Rooms.rooms[roomid].number; i++)
            {
                if (Rooms.rooms[roomid].users[i].state == state.ADD &&
                        Rooms.rooms[roomid].users[i].name == uname)
                    Rooms.rooms[roomid].users[i].state = state.READY;
                else if (Rooms.rooms[roomid].users[i].state != state.READY)
                    flag++;
            }
            if (flag > 0)
                return "no";
            return "yes";
        }

        //
        // GET: /Question/

        public ActionResult Index()
        {
            return View(db.Questions.ToList());
        }

        //
        // GET: /Question/Details/5

        public ActionResult Details(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // GET: /Question/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Question/Create

        [HttpPost]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Questions.Add(question);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(question);
        }

        //
        // GET: /Question/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Question/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(question);
        }

        //
        // GET: /Question/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Question/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Question question = db.Questions.Find(id);
            db.Questions.Remove(question);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}